//
//  FBMenuViewController.m
//  FindBook
//
//  Created by zhaoxu_liu on 13-11-1.
//  Copyright (c) 2013年 lzx. All rights reserved.
//

#import "FBMenuViewController.h"
#import "FBMenuModel.h"
#import "FBMenuData.h"
#import <QuartzCore/QuartzCore.h>

@interface FBMenuViewController ()<UITableViewDelegate, UITableViewDataSource>
{
    FBMenuModel *_menuModel;
    UITableView *_tableView;
    NSArray *_tableViewData;
}

@end

@implementation FBMenuViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    if (_menuModel == nil) {
        _menuModel = [[FBMenuModel alloc] initWithDelegate:self];
    }
    
    [_menuModel cancelRequest];
    [_menuModel sendRequest];
    
    [self loadCommpentsView];
	// Do any additional setup after loading the view.
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [_menuModel cancelRequest];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)loadCommpentsView
{
    if (_tableView == nil) {
        _tableView = [[UITableView alloc] initWithFrame:self.view.bounds];
        
        if (IOS_VERSION < 7.0) {
            _tableView.frame = CGRectMake(0, 20, 320, 460);
        }
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorColor = [UIColor colorWithRed:150/255.0f green:161/255.0f blue:177/255.0f alpha:1.0f];
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.tableHeaderView = ({
            UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 184.0f)];
            UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 40, 100, 100)];
            imageView.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
            imageView.image = [UIImage imageNamed:@""];
            imageView.layer.masksToBounds = YES;
            imageView.layer.cornerRadius = 50.0;
            imageView.layer.borderColor = [UIColor whiteColor].CGColor;
            imageView.layer.borderWidth = 3.0f;
            imageView.layer.rasterizationScale = [UIScreen mainScreen].scale;
            imageView.layer.shouldRasterize = YES;
            imageView.clipsToBounds = YES;
            
            UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 150, 250, 24)];
            label.text = @"目  录";
            label.textAlignment = NSTextAlignmentCenter;
            label.font = [UIFont fontWithName:@"HelveticaNeue" size:21];
            label.backgroundColor = [UIColor clearColor];
            label.textColor = [UIColor colorWithRed:62/255.0f green:68/255.0f blue:75/255.0f alpha:1.0f];
            [label sizeToFit];
            label.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
            
            [view addSubview:imageView];
            [view addSubview:label];
            view;
        });
        
        [self.view addSubview:_tableView];
    }
    else{
        [_tableView reloadData];
    }
    
}

- (void)didHTTPRequestModelFinished:(RYCHTTPRequestModel *)aModel
{
    [super didHTTPRequestModelFinished:aModel];
    if (aModel == _menuModel) {
        _tableViewData = _menuModel.ewRspData.result;
        
        [self loadCommpentsView];

    }
}


#pragma mark - tableView delegate
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    cell.backgroundColor = [UIColor clearColor];
    cell.textLabel.textColor = [UIColor colorWithRed:62/255.0f green:68/255.0f blue:75/255.0f alpha:1.0f];
    cell.textLabel.font = [UIFont fontWithName:@"HelveticaNeue" size:15];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 44;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _tableViewData.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString* reuseID = @"normalCell";
    UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:reuseID];
    if (nil == cell)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuseID];
        cell.accessoryType = UITableViewCellAccessoryNone;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    FBMenuData *menuData = [_tableViewData objectAtIndex:indexPath.row];
    cell.textLabel.text = menuData.catalog;
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    FBMenuData *menuData = [_tableViewData objectAtIndex:indexPath.row];
    NSDictionary *userInfo = [NSDictionary dictionaryWithObjectsAndKeys:menuData.ID, @"catalogID", menuData.catalog, @"catalog", nil];
    [[NSNotificationCenter defaultCenter] postNotificationName:FBLOADBOOKSNOTIFICATION object:nil userInfo:userInfo];
    [self.frostedViewController hideMenuViewController];
}

@end
